package editeurimpl;





import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

import Debog.Debog;
import editeur.Editeur;

public class EditeurImpl extends Debog implements Editeur
{
        protected Selection selection;
        
        private PressePapier pressepapier;
        
        protected Buffer buffer;

        private String filename;
        
        
        public EditeurImpl(Selection sel,Buffer buf,PressePapier pp){
                selection=sel;
                buffer=buf;
                pressepapier=pp;
        }
        public void copier(){
                if (selection.active()) {
                 pressepapier.setcontenu( buffer.gettext( selection.getdebut(), selection.getfin() ) );
                }
        }
        public int coller(int pos){
                String t =pressepapier.getcontenu();
                buffer.inserer(t,pos);
                return t.length();
        }
        public void effacer() {
                buffer.effacer(selection.getdebut(),selection.getfin());
        }
        public void inserer( String t ,int pos ){
                buffer.inserer(pos,t);
        }
        public void selectionner( int deb, int fin ){
                if (deb >= 0 ){
                        if ((buffer.length() >= deb) ) {
                                selection.setdebut(deb);
                        }
                        else { selection.setdebut(buffer.length());}
                        if (fin>deb){
                                if (buffer.length() >= fin) {
                                        selection.setfin(fin);
                                }
                                else { selection.setfin(buffer.length());}

                        }
                }
        }
        public boolean enregistrer(String file) {
                FileWriter fw;
                boolean ok=true;
                debog("try to write : "+file);
                try {	
                        fw=new FileWriter(file);
                        BufferedWriter b = new BufferedWriter(fw);
                        b.write(buffer.toString());
                        b.close();
                        filename=file;
                } catch (IOException e) {
                        e.printStackTrace();
                        ok=false;
                }
                return ok;
        }
        public boolean ouvrir(String file) {
                boolean ok=true;
                FileReader fr;
                BufferedReader b =null;
                debog("try to open : "+file);
                try {
                		
                        fr = new FileReader(file);
                        b = new BufferedReader(fr);
                        


                        filename=file;
                } catch (FileNotFoundException e) {
                        ok=false;
                }
                if (ok) {
                        buffer=new Buffer();
                        synchronized (b){
                                try {
                                        int pos=0;
                                        String car=b.readLine();
                                        while (car!=null){
                                        		
                                        		buffer.inserer(pos,car+'\n');
                                        		pos+=car.length()+1;
                                        		car=b.readLine();
	                                            
                                        }                                  
                                } catch (IOException e) {
                                        e.printStackTrace();
                                        ok=false;
                                }
                        }
                }

                return ok;
        }
        public boolean ouvrir(String file,String encodage) {
            boolean ok=true;
            FileReader fr;
            BufferedReader b =null;
            debog("try to open : "+file);
            try {
            		
                    fr = new FileReader(file);
                    b = new BufferedReader(fr);

                    filename=file;
            } catch (FileNotFoundException e) {
                    ok=false;
            }
            if (ok) {
                    buffer=new Buffer();
                    synchronized (b){
                            try {
                                    int pos=0;
                                    String car=b.readLine();
                                    while (car!=null){
   
                                    		car=new String(car.getBytes() ,encodage);
                                    		buffer.inserer(pos,car+'\n');
                                    		pos+=car.length()+1;
                                    		car=b.readLine();
                                            
                                    }                                  
                            } catch (IOException e) {
                                    e.printStackTrace();
                                    ok=false;
                            }
                    }
            }
            return ok;
    }
        public String get_buffer_contenu() {
                return buffer.toString();
        }
        public int get_buffer_taille() {
                return buffer.length();
        }
        public String get_selection_contenu() {
                if (selection.active()&&selection.getfin()<=buffer.length()){ 
                return buffer.gettext(get_selection_deb(),get_selection_fin());
                }
                return "";
        }
        public int get_selection_deb() {
                return selection.getdebut();
        }
        public int get_selection_fin() {
                return selection.getfin();
        }
        public String gettitre() { 
                return filename;
        }
        public boolean selection_active() {
                return selection.active();
        }
        public void selection_desactiver() {
                selection.desactiver();
        }
        public int get_selection_size() {
                return selection.gettaille();
        }
		public boolean recharger() {

			if (filename!=null){return ouvrir(filename);}
			return false;
		}
		public boolean recharger(String enc) {

			if (filename!=null){return ouvrir(filename,enc);}
			return false;
		}
        

}